﻿using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infrastructure.Cache;
using Domain.Model;
using TencentCloud.Bm.V20180423.Models;

namespace Domain.Config
{
    public static class SystemConfig
    {        
        static SystemConfig()
        {
            memoryCacheService = ServiceProvide.Get<IMemoryCacheService>();
        }
        public static IMemoryCacheService memoryCacheService { get; set; }
        static Dictionary<string, object> Configs = new Dictionary<string, object>();

        public static T GetConfigByTime<T>(string groupName, string name, TimeSpan timeSpan)
        {
            object obj = memoryCacheService.Get<object>($"SystemConfig:{groupName}.{name}");
            if (obj != null) return (T)Convert.ChangeType(obj, typeof(T));

            var dic = ServiceProvide.Get<IRepositorys.IDicRepository>();
            var entity = dic.Entities.FirstOrDefault(i => i.GroupName == groupName && i.KeyName == name);
            if (entity == null) return default(T);

            var f = (T)Convert.ChangeType(entity.KeyValue, typeof(T));
            memoryCacheService.Set<T>($"SystemConfig:{groupName}.{name}", f, timeSpan);

            return f;
        }


        static T loadConfig<T>(params string[][] strArr)
        {
            var dic = ServiceProvide.Get<IRepositorys.IDicRepository>();
            foreach (var item in strArr)
            {
                var groupName = item[0];
                var name = item[1];
                var entity = dic.Entities.FirstOrDefault(i => i.GroupName == groupName && i.KeyName == name);
                if (entity != null)
                {
                    SetConfigName(groupName, name, entity.KeyValue);

                    return (T)Convert.ChangeType(entity.KeyValue, typeof(T));
                }
            }
           
            return default(T);
        }

        public static T GetConfigByLevelName<T>(string groupName, string name)
        {
            if (Configs.TryGetValue($"{groupName}.{name}", out object val))
            {
                return (T)Convert.ChangeType(val, typeof(T));
            }

            if (Configs.TryGetValue($"基础配置.{name}", out object val2))
            {
                return (T)Convert.ChangeType(val2, typeof(T));
            }

            return default(T);
            //return loadConfig<T>(new string[][] { new string[] { groupName, name }, new string[] { "基础配置", name } });
        }

        public static void SetConfigName(string groupName, string name, string val)
        {
            Configs.TryAdd(
                    $"{groupName}.{name}",
                    val
                );
        }

        public static List<Tuple<string, string>> GetConfigListByName(string name)
        {
            switch (name)
            {
                default:
                case "系统配置":
                    return GetSystemConfig();
                case "基础配置":
                    return GetBaseConfig();
                case "超级商人":
                    return Get超级商人();
                case "任务达人":
                    return Get任务达人();
            }
        }

        static List<Tuple<string, string>> GetSystemConfig()
        {
            List<Tuple<string, string>> vs = new List<Tuple<string, string>>();

            vs.Add(Tuple.Create("Hmac开启", "True"));
            vs.Add(Tuple.Create("HmacSecret", "Izt981sZ"));
            vs.Add(Tuple.Create("HmacUserName", "UserHmac"));
            vs.Add(Tuple.Create("HmacClockSkew", "300"));


            vs.Add(Tuple.Create("DeviceTokenAesKey", "jbelmnxinvlreanb"));
            vs.Add(Tuple.Create("DeviceTokenAesIV", "5992039507930310"));


            vs.Add(Tuple.Create("等级.超级商人.1个月", "6800"));
            vs.Add(Tuple.Create("等级.超级商人.3个月", "18800"));
            vs.Add(Tuple.Create("等级.超级商人.6个月", "36800"));
            vs.Add(Tuple.Create("等级.超级商人.12个月", "69800"));

            vs.Add(Tuple.Create("等级.任务达人.1个月", "1680"));

            vs.Add(Tuple.Create("新手任务配置.1级任务数", "1"));
            vs.Add(Tuple.Create("新手任务配置.1级奖励", "30"));

            vs.Add(Tuple.Create("新手任务配置.2级任务数", "3"));
            vs.Add(Tuple.Create("新手任务配置.2级奖励", "50"));

            vs.Add(Tuple.Create("新手任务配置.3级任务数", "16"));
            vs.Add(Tuple.Create("新手任务配置.3级奖励", "220"));


            vs.Add(Tuple.Create("十二元现金奖励.好友首次完成任务", "300"));
            vs.Add(Tuple.Create("十二元现金奖励.好友首次提现", "100"));
            vs.Add(Tuple.Create("十二元现金奖励.好友第二提现", "150"));
            vs.Add(Tuple.Create("十二元现金奖励.好友第三提现", "200"));
            vs.Add(Tuple.Create("十二元现金奖励.好友第四提现", "250"));
            vs.Add(Tuple.Create("十二元现金奖励.好友第五提现", "300"));

            vs.Add(Tuple.Create("奖励排行.1", "388800"));
            vs.Add(Tuple.Create("奖励排行.2", "288800"));
            vs.Add(Tuple.Create("奖励排行.3", "188800"));
            vs.Add(Tuple.Create("奖励排行.4", "88800"));
            vs.Add(Tuple.Create("奖励排行.5", "68800"));
            vs.Add(Tuple.Create("奖励排行.6", "58800"));
            vs.Add(Tuple.Create("奖励排行.7", "38800"));
            vs.Add(Tuple.Create("奖励排行.8", "28800"));
            vs.Add(Tuple.Create("奖励排行.9", "18800"));
            vs.Add(Tuple.Create("奖励排行.10", "8800"));

            vs.Add(Tuple.Create("推荐排行.1", "1888800"));
            vs.Add(Tuple.Create("推荐排行.2", "1588800"));
            vs.Add(Tuple.Create("推荐排行.3", "1088800"));
            vs.Add(Tuple.Create("推荐排行.4", "688800"));
            vs.Add(Tuple.Create("推荐排行.5", "588800"));
            vs.Add(Tuple.Create("推荐排行.6", "488800"));
            vs.Add(Tuple.Create("推荐排行.7", "388800"));
            vs.Add(Tuple.Create("推荐排行.8", "288800"));
            vs.Add(Tuple.Create("推荐排行.9", "188800"));
            vs.Add(Tuple.Create("推荐排行.10", "88800"));


            //页面配置
            vs.Add(Tuple.Create("page.showAward", "1"));
            vs.Add(Tuple.Create("page.showRecommend", "1"));

            //vs.Add(Tuple.Create("UserTokenAesKey", "xoijrognzldsfhos"));
            //vs.Add(Tuple.Create("UserTokenAesIV", "1564538575468743"));
            return vs;
        }

        static List<Tuple<string, string>> GetBaseConfig()
        {
            List<Tuple<string, string>> vs = new List<Tuple<string, string>>();

            vs.Add(Tuple.Create("一级下线升级超级商人收益", "30"));
            vs.Add(Tuple.Create("一级下线刷新道具佣金收益", "10"));
            vs.Add(Tuple.Create("一级下线推荐道具佣金收益", "10"));

            vs.Add(Tuple.Create("佣金配置.任务预留佣金百分比", "20"));

            vs.Add(Tuple.Create("提现配置.最低提现金额", "500"));
            vs.Add(Tuple.Create("提现配置.每天提现次数", "1"));
            vs.Add(Tuple.Create("提现配置.每笔最大金额", "50000"));
            vs.Add(Tuple.Create("提现配置.大于多少金额需要审核", "0"));//vs.Add(Tuple.Create("提现配置.大于多少金额需要审核", "5000"));
            vs.Add(Tuple.Create("提现配置.提现手续费", "3"));

            vs.Add(Tuple.Create("完成任务收益", "80"));
            vs.Add(Tuple.Create("一级下线完成任务佣金收益", "8"));
            vs.Add(Tuple.Create("二级下线完成任务佣金收益", "4"));
            vs.Add(Tuple.Create("每天发布最大任务数", "100"));
            vs.Add(Tuple.Create("任务上推荐减免比例", "100"));
            vs.Add(Tuple.Create("任务上推荐费用", "1000"));
            vs.Add(Tuple.Create("任务上推荐费用[小时]", "1"));//1小时
            vs.Add(Tuple.Create("用户领取最大任务数", "3"));

            return vs;
        }

        static List<Tuple<string, string>> Get超级商人()
        {
            List<Tuple<string, string>> vs = new List<Tuple<string, string>>();

            vs.Add(Tuple.Create("提现配置.提现手续费", "3"));
            vs.Add(Tuple.Create("完成任务收益", "100"));
            vs.Add(Tuple.Create("每天发布最大任务数", "100"));
            vs.Add(Tuple.Create("任务上推荐减免比例", "50"));

            return vs;
        }

        static List<Tuple<string, string>> Get任务达人()
        {
            List<Tuple<string, string>> vs = new List<Tuple<string, string>>();

            vs.Add(Tuple.Create("完成任务收益", "100"));

            return vs;
        }
    }
}
